/*
 * JsonRpcInterface.h
 *
 *  Created on: Mar 16, 2017
 *      Author: zhangjf
 */

#ifndef SRC_JSONRPCINTERFACE_H_
#define SRC_JSONRPCINTERFACE_H_

#include <map>
#include <string>
#include <functional>
#include <Poco/AutoPtr.h>
#include <Poco/JSON/Object.h>
#include <Poco/Dynamic/Var.h>
#include <Poco/RefCountedObject.h>

using namespace Poco;
using namespace Poco::JSON;
using namespace Poco::Dynamic;

class WebCallInterface : public RefCountedObject
{
public:
	typedef AutoPtr<WebCallInterface> Ptr;

	WebCallInterface(const std::string& name, Object::Ptr& joParams);
	virtual ~WebCallInterface();

	virtual bool preprocess(std::string& strReq) { return true; }
	virtual bool process();
	virtual bool postprocess(std::string& strResp) { return true; }

	virtual Object& getResult() { return _joResult; }
	virtual Object& getError() { return _joError; }

	static UInt32 getSeqID() { static UInt32 iReqID = 0; return ++iReqID; }

protected:
	Object::Ptr& _joParams;
	Object _joResult;
	Object _joError;
	std::string _strMethodName;

	static const std::string _strMethod;
	static const std::string _strJsonRpc;
	static const std::string _strParams;
	static const std::string _strID;
};

class WebCallInterfaceFactory
{
public:
	static WebCallInterface::Ptr createInterface(std::string& interfacee, Object::Ptr& joParams);

private:
	static std::map<std::string, std::function<WebCallInterface::Ptr(Object::Ptr&)>> mapJRI;
	static void initialize();
};

#endif /* SRC_JSONRPCINTERFACE_H_ */
